package com.frxuexi.mdm2202.ui

import androidx.compose.material.icons.Icons
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.lib_base.NavPath
import com.frxuexi.mdm2202.R


sealed class NavItem(val router: String, val title: String, val img: Int) {
    object MessageNav : NavItem(NavPath.MESSAGES, "消息", R.drawable.icon1275)
    object HomeNav : NavItem(NavPath.HOME, "门户", R.drawable.icon64)
    object MineNav : NavItem(NavPath.MINE, "我", R.drawable.icon64)

}

@Composable
fun MainPage(navController: NavHostController) {
    val list = listOf(NavItem.MessageNav, NavItem.HomeNav, NavItem.MineNav)
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination
    Scaffold(bottomBar = {
        if(currentDestination?.hierarchy?.any { list.map {
            navItem ->
            navItem.router
            }.contains(it.route) }==true) {
        NavigationBar {
            list.forEach { nav ->
                NavigationBarItem(selected = currentDestination?.hierarchy?.any { it.route == nav.router} == true,
                    onClick = {
                    navController.navigate(nav.router) {
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }, icon = {
                    Icon(
                        painter = painterResource(id = nav.img),
                        contentDescription = ""
                    )
                }, label = { Text(text = nav.title) })
            }
        }
        }
    }) {
        MyNavHost(navController = navController, it)
    }

}